package fr.umlv.m2.recipes.ideas.sqlite.db.objects;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import fr.umlv.m2.recipes.ideas.dao.IngredientDAO;
import fr.umlv.m2.recipes.ideas.dao.KeywordDAO;
import fr.umlv.m2.recipes.ideas.dao.RecipeDAO;
import fr.umlv.m2.recipes.ideas.dao.RecipeToIngredientDAO;
import fr.umlv.m2.recipes.ideas.dao.RecipeToKeywordDAO;
import fr.umlv.m2.recipes.ideas.dao.StepDAO;
import fr.umlv.m2.recipes.ideas.sqlite.SQLiteDbRecipes;
/**
 * Object use to create the database
 * @author Christophe Chillan
 *
 */
public class RecipesDB {
	private static final int DB_VERSION = 1;
	private static final String DB_NAME = "recipes.db";
	private static final RecipesDB instance = new RecipesDB();
	
	
	private RecipesDB(){}
	
	
	private static SQLiteDatabase db;
	private static SQLiteDbRecipes sqliteRecipesDB;
	
	/**
	 * Get a {@link RecipesDB} instance
	 * @return the RecipesDB singleton instance
	 */
	public static RecipesDB getInstance(){
		return instance;
	}
	
	/**
	 * Create a database instance from the context
	 * @param context
	 */
	public static void createDBInstance(Context context){
		sqliteRecipesDB = new SQLiteDbRecipes(context, DB_NAME, null, DB_VERSION);
	}
	
	/**
	 * Prepare the DB to write
	 */
	public static void open(){
		db = sqliteRecipesDB.getWritableDatabase();
	}
	
	/**
	 * Verify if the database is open
	 * @return true if the database is open, false otherwise
	 */
	public static boolean isOpen(){
		return db.isOpen();
	}
	
	
	/**
	 * Close the database
	 */
	public static void close(){
		db.close();
	}
	
	/**
	 * Give the Recipes DB
	 * @return {@link SQLiteDatabase}
	 */
	public static SQLiteDatabase getDatabase(){
		return db;
	}
	
	/**
	 * Get a {@link RecipeDAO}
	 * @return a {@link RecipeDAO}
	 */
	public static RecipeDAO getRecipeDao(){
		return sqliteRecipesDB.getRecipesDao();
	}
	
	/**
	 * Get a {@link IngredientDAO}
	 * @return {@link IngredientDAO}
	 */
	public static IngredientDAO getIngredientDao(){
		return sqliteRecipesDB.getIngredientDao();
	}
	
	/**
	 * Get a {@link StepDAO}
	 * @return {@link StepDAO}
	 */
	public static StepDAO getStepDao(){
		return sqliteRecipesDB.getStepDao();
	}
	
	/**
	 * Get a {@link KeywordDAO}
	 * @return {@link KeywordDAO}
	 */
	public static KeywordDAO getKeywordDao(){
		return sqliteRecipesDB.getKeywordDao();
	}
	
	/**
	 * Get a {@link RecipeToKeywordDAO}
	 * @return  {@link RecipeToKeywordDAO}
	 */
	public static RecipeToKeywordDAO getRecipesToKeywordDao(){
		return sqliteRecipesDB.getRecipesToKeywordDao();
	}
	
	/**
	 * Get a {@link RecipeToIngredientDAO}
	 * @return {@link RecipeToIngredientDAO}
	 */
	public static RecipeToIngredientDAO getRecipesToIngredientDao(){
		return sqliteRecipesDB.getRecipesToIngredientDao();
	}
	




}
